package com.sensePlusVR.activity.guesture;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.widget.guesture.GestureContentView;
import com.sensePlusVR.widget.guesture.GestureDrawline.GestureCallBack;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


/**
 * 
 * @author: Loren
 * @create: 2016-7-20上午10:25:17
 * @desc: 手势密码设置界面
 *
 */
@SuppressWarnings({"unused"})
@ContentView(R.layout.activity_gesture_edit)
public class GestureEditActivity extends BaseActivity{
	private static final String TAG = GestureEditActivity.class.getSimpleName();
	/** 手机号码*/
	public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER";
	/** 意图 */
	public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE";
	/** 首次提示绘制手势密码，可以选择跳过 */
	public static final String PARAM_IS_FIRST_ADVICE = "PARAM_IS_FIRST_ADVICE";
	
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;
	
	@ViewInject(R.id.text_tip)
	private TextView mTextTip;
	
	//@ViewInject(R.id.lock_indicator)
	//private LockIndicator mLockIndicator;
	
	@ViewInject(R.id.gesture_container)
	private FrameLayout mGestureContainer;
	
	//@ViewInject(R.id.text_reset)
	//private TextView mTextReset;
	
	private GestureContentView mGestureContentView;
	private String mParamSetUpcode = null;
	private String mParamPhoneNumber;
	private boolean mIsFirstInput = true;
	private String mFirstPassword = null;
	private String mConfirmPassword = null;
	private int mParamIntentCode;
	
	private boolean isFirstReset;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}
	
	@Override
	public void getIntentData() {
		super.getIntentData();
		isFirstReset = MyPageJump.pageJumpGetBoolean(this, Constant.INTENT_GUESTURE_FIRST_RESET);
	}
	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);
		title_bar_title_tv.setText(R.string.app_start_password);
		if(isFirstReset)
		mTextTip.setText(R.string.guesture_reset_tip2);
		else
		mTextTip.setText(R.string.guesture_first_tip1);
	}
	@Override
	public void initData() {
		super.initData();
		// 初始化一个显示各个点的viewGroup
		mGestureContentView = new GestureContentView(this, false, "", new GestureCallBack() {
			@Override
			public void onGestureCodeInput(String inputCode) {
				Log.d(TAG, "input code : " + inputCode);
				if (!isInputPassValidate(inputCode)) {
					mTextTip.setTextColor(getResources().getColor(R.color.app_text_color_gray1));
					mTextTip.setText(R.string.guesture_error_less_four);
					// 左右移动动画
					Animation shakeAnimation = AnimationUtils.loadAnimation(GestureEditActivity.this, R.anim.shake);
					mTextTip.startAnimation(shakeAnimation);
					// 保持绘制的线，1.5秒后清除
					mGestureContentView.clearDrawlineState(1300L);
					return;
				}
				if (mIsFirstInput) {
					mFirstPassword = inputCode;
					mGestureContentView.clearDrawlineState(0L);
					if(isFirstReset)
					mTextTip.setText(R.string.guesture_reset_tip3);
					else
					mTextTip.setText(R.string.guesture_first_tip2);
				} else {
					if (inputCode.equals(mFirstPassword)) {
							//MyToast.showMessage(GestureEditActivity.this, R.string.guesture_set_success);
							MySharedPreferences.save2(GestureEditActivity.this,MySharedPreferences.NAME_NOT_CLEAR, Constant.SP_INPUTCODE, inputCode);
						mGestureContentView.clearDrawlineState(0L);
						//第一次时先进入设置手势密码
						boolean isFirstInstall = MySharedPreferences.getBoolean2(GestureEditActivity.this,MySharedPreferences.NAME_NOT_CLEAR, Constant.IS_FIRST_GUESTURE_EDIT);
						if(!isFirstInstall){
							MySharedPreferences.save2(GestureEditActivity.this,MySharedPreferences.NAME_NOT_CLEAR, Constant.IS_FIRST_GUESTURE_EDIT, true);
						}
						AppManager.getInstance().finishActivity(GestureEditActivity.this);
					} else {
						mTextTip.setTextColor(getResources().getColor(R.color.app_text_color_gray1));
						mTextTip.setText(R.string.guesture_psw_not_same);
						// 左右移动动画
						Animation shakeAnimation = AnimationUtils.loadAnimation(GestureEditActivity.this, R.anim.shake);
						mTextTip.startAnimation(shakeAnimation);
						// 保持绘制的线，1.5秒后清除
						mGestureContentView.clearDrawlineState(1300L);
					}
				}
				mIsFirstInput = false;
			}

			@Override
			public void checkedSuccess() {
				Log.d(TAG, "checkedSuccess...");
			}

			@Override
			public void checkedFail() {
				Log.d(TAG, "checkedFail...");
			}
		});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(mGestureContainer);
		//updateCodeList("");
	}
	
/*	private void updateCodeList(String inputCode) {
		// 更新选择的图案
		mLockIndicator.setPath(inputCode);
	}*/
	
	@Event(value = {R.id.text_cancel,/*R.id.text_reset,*/R.id.title_bar_left_ib})
	private void setViewOnclick(View view){
		switch (view.getId()) {
		case R.id.text_cancel:
			AppManager.getInstance().finishActivity(this);
			break;
		/*case R.id.text_reset:
			mIsFirstInput = true;
			updateCodeList(Constant.STR_EMPTY);
			mTextTip.setText(getString(R.string.guesture_set_pattern));
			break;*/
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;
		default:
			break;
		}
	}
	
	private boolean isInputPassValidate(String inputPassword) {
		if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < Constant.NUMBER_4) {
			return false;
		}
		return true;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
}
